package com.jeesite.modules.task.dceconomicindicator.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.utils.OfficeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.task.dceconomicindicator.entity.DcEconomicIndicator;
import com.jeesite.modules.task.dceconomicindicator.service.DcEconomicIndicatorService;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 经济指标Controller
 * @author xhc
 * @version 2022-10-20
 */
@Controller
@RequestMapping(value = "${adminPath}/dceconomicindicator/dcEconomicIndicator")
public class DcEconomicIndicatorController extends BaseController {

	@Autowired
	private DcEconomicIndicatorService dcEconomicIndicatorService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcEconomicIndicator get(String id, boolean isNewRecord) {
		return dcEconomicIndicatorService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("dceconomicindicator:dcEconomicIndicator:view")
	@RequestMapping(value = {"list", ""})
	public String list(DcEconomicIndicator dcEconomicIndicator, Model model) {
		model.addAttribute("dcEconomicIndicator", dcEconomicIndicator);
		List<DcEconomicIndicator> list = dcEconomicIndicatorService.findList(dcEconomicIndicator);
		if(list!=null && list.size()>0){
			DcEconomicIndicator dcEconomicIndicator1 = list.get(0);
			Map map = JSONObject.parseObject(dcEconomicIndicator1.getJsonColumnData(),Map.class);
			Map mapTwo = JSONObject.parseObject(dcEconomicIndicator1.getJsonColumnTwoData(),Map.class);
			model.addAttribute("columns", map);
			model.addAttribute("columnTwos", mapTwo);
			model.addAttribute("columnTotal", 	map.keySet().size());
		}
		return "modules/task/dceconomicindicator/dcEconomicIndicatorList";
	}

	/**
	 * 查询主要经济指标排名
	 * @return
	 */
	@RequestMapping("/listColumn")
	public Map listColumn(DcEconomicIndicator dcEconomicIndicator){
		Map maps = new HashMap();
		List<DcEconomicIndicator> list = dcEconomicIndicatorService.findList(dcEconomicIndicator);
		if(list!=null && list.size()>0){
			DcEconomicIndicator dcEconomicIndicator1 = list.get(0);
			Map map = JSONObject.parseObject(dcEconomicIndicator1.getJsonColumnData(),Map.class);
			Map mapTwo = JSONObject.parseObject(dcEconomicIndicator1.getJsonColumnTwoData(),Map.class);
			maps.put("columnss", map);
			maps.put("columnTwoss", mapTwo);
			maps.put("columnTotals", 	map.keySet().size());
		}
		return maps;
	}



	/**
	 * 主要经济指标全省排名
	 */
	@RequestMapping(value = "index")
	public String leaderHomeProvinceIndex(DcEconomicIndicator dcEconomicIndicator,Model model) {
		List<DcEconomicIndicator> list = dcEconomicIndicatorService.findList(dcEconomicIndicator);
		List< Map> titles = list.stream().map(x -> {
			Map map = new HashMap();
			map.put("code",x.getTitle());
			map.put("name",x.getTitle());
			return map;
		}).collect(Collectors.toList());
		model.addAttribute("titles", titles);
		return "modules/task/dceconomicindicator/dcEconomicIndictiorIndex";
	}
	/**
	 * 主要经济指标全省排名
	 */
	@RequestMapping(value = "index2")
	public String index2(DcEconomicIndicator dcEconomicIndicator,Model model) {
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		return "modules/task/dceconomicindicator/dcEconomicIndictiorIndex2";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("dceconomicindicator:dcEconomicIndicator:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Map<String,String>> listData(DcEconomicIndicator dcEconomicIndicator, HttpServletRequest request, HttpServletResponse response) {
		Page<Map<String,String>> page = new Page<>();
		List<DcEconomicIndicator> list = dcEconomicIndicatorService.findList(dcEconomicIndicator);
		DcEconomicIndicator dcEconomicIndicator1 = list.get(0);
		List list_ = JSONObject.parseObject(dcEconomicIndicator1.getJsonData(),List.class);
		list_.remove(0);
		list_.remove(0);
		page.setList(list_);
		page.setCount(list_.size());
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("dceconomicindicator:dcEconomicIndicator:view")
	@RequestMapping(value = "form")
	public String form(DcEconomicIndicator dcEconomicIndicator, Model model) {
		model.addAttribute("dcEconomicIndicator", dcEconomicIndicator);
		return "modules/task/dceconomicindicator/dcEconomicIndicatorForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("dceconomicindicator:dcEconomicIndicator:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcEconomicIndicator dcEconomicIndicator) {

		dcEconomicIndicatorService.save(dcEconomicIndicator);
		return renderResult(Global.TRUE, text("保存经济指标成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("dceconomicindicator:dcEconomicIndicator:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(DcEconomicIndicator dcEconomicIndicator) {
		dcEconomicIndicator.setStatus(DcEconomicIndicator.STATUS_DISABLE);
		dcEconomicIndicatorService.updateStatus(dcEconomicIndicator);
		return renderResult(Global.TRUE, text("停用经济指标成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("dceconomicindicator:dcEconomicIndicator:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(DcEconomicIndicator dcEconomicIndicator) {
		dcEconomicIndicator.setStatus(DcEconomicIndicator.STATUS_NORMAL);
		dcEconomicIndicatorService.updateStatus(dcEconomicIndicator);
		return renderResult(Global.TRUE, text("启用经济指标成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("dceconomicindicator:dcEconomicIndicator:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcEconomicIndicator dcEconomicIndicator) {
		dcEconomicIndicatorService.delete(dcEconomicIndicator);
		return renderResult(Global.TRUE, text("删除经济指标成功！"));
	}


//	/**
//	 * 导入Excel信息
//	 * @param file
//	 * @return
//	 */
//	@RequestMapping("/importInfo")
//	@ResponseBody
//	public String infoImport(MultipartFile file) {
//
//		try {
//			dcEconomicIndicatorService.infoImport(file);
//			return renderResult(Global.TRUE, "导入成功");
//		} catch (IOException e) {
//			e.printStackTrace();
//			return renderResult(Global.FALSE, "导入失败 :"+e.getMessage());
//		}
//	}

	/**
	 * 导入Excel信息
	 * @param file
	 * @return
	 */
	@RequestMapping("/importInfo2")
	@ResponseBody
	public String infoImport2(MultipartFile file,String selectMonth,String selectYear) {

		try {
			dcEconomicIndicatorService.infoImport2(file,selectMonth,selectYear);
			return renderResult(Global.TRUE, "导入成功");
		} catch (IOException e) {
			e.printStackTrace();
			return renderResult(Global.FALSE, "导入失败 :"+e.getMessage());
		}
	}


	/**
	 * 查询主要经济指标排名
	 * @return
	 */
	@RequestMapping("/getRank")
	public Map getProvinceMajorRank(String title){
		Map map = dcEconomicIndicatorService.getProvinceMajorRank(title);
		return map;
	}

	/**
	 * 查询主要扣分排名
	 * @return
	 */
	@RequestMapping("/getScore")
	public Map getScore(){
		Map map = dcEconomicIndicatorService.getScore();
		return map;
	}

	@GetMapping(value = "/importTemplate")
	public void exportFile(String importType, HttpServletResponse response) {
		// fileName = "ImportModel.xlsx";
		 String fileName ="";

		try {
			//获取要下载的模板名称
			if(StringUtils.equals(importType,"1")){
				fileName = "经济运行考核排名模板.xlsx";
			}
			if(StringUtils.equals(importType,"2")){
				fileName = "经济指标增速排名模板.xlsx";
			}
			if(StringUtils.equals(importType,"3")){
				fileName = "经济指标绝对值排名模板.xlsx";
			}
			//设置要下载的文件的名称
			response.setHeader("Content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "utf-8"));
			//通知客服文件的MIME类型
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			//获取文件的路径
			String filePath = getClass().getResource("/template/" + fileName).getPath();
			filePath = java.net.URLDecoder.decode(filePath, "utf-8");
			FileInputStream input = new FileInputStream(filePath);
			OutputStream out = response.getOutputStream();
			byte[] b = new byte[2048];
			int len;
			while ((len = input.read(b)) != -1) {
				out.write(b, 0, len);
			}
			//修正 Excel在“xxx.xlsx”中发现不可读取的内容。是否恢复此工作薄的内容？如果信任此工作簿的来源，请点击"是"
			response.setHeader("Content-Length", String.valueOf(input.getChannel().size()));
			input.close();
		} catch (Exception ex) {
			logger.error("getApplicationTemplate :", ex);
		}
	}
	/**
	 * 查询1
	 * @return
	 */
	@RequestMapping("/getRank1")
	public Map getRank1(String leftParam,String selectYear,String selectMonth,String importType){
		Map map = dcEconomicIndicatorService.getRank1(leftParam,selectYear,selectMonth,importType);
		return map;
	}
	/**
	 * 查询column
	 * @return
	 */
	@RequestMapping("/getColumn")
	public Set<String> getColumn(String selectYear,String selectMonth,String importType){
		Set<String> list = dcEconomicIndicatorService.getColumn(selectYear,selectMonth,importType);
		return list;
	}

	/**
	 * 导入Excel信息
	 * @param file
	 * @return
	 */
	@RequestMapping("/importInfo")
	@ResponseBody
	public String infoImport(MultipartFile file,String selectMonth,String selectYear,String importType) {

		try {
			if(StringUtils.equals(importType,"1")){
				dcEconomicIndicatorService.infoImport2(file,selectMonth,selectYear);
			}
			if(StringUtils.equals(importType,"2")){
				dcEconomicIndicatorService.infoImport3(file,selectMonth,selectYear);
			}
			if(StringUtils.equals(importType,"3")){
				dcEconomicIndicatorService.infoImport4(file,selectMonth,selectYear);
			}
			return renderResult(Global.TRUE, "导入成功");
		} catch (IOException e) {
			e.printStackTrace();
			return renderResult(Global.FALSE, "导入失败 :"+e.getMessage());
		}
	}

}